layui.use(['layer', 'form', 'admin', 'ax', 'laytpl'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laytpl = layui.laytpl;

    var id = system.getUrlParam('id');

    //获取菜单信息
    var ajaxData = new $ax(system.contextPath + "/api/isv.info.get");
    ajaxData.setType("get");
    ajaxData.set({id: id});
    var resultData = ajaxData.start();
    form.val('formContainer', resultData.data);

    var roleCode = [];
    var roleList = resultData.data.roleList;
    for (var i = 0; i < roleList.length; i++) {
        roleCode.push(roleList[i].roleCode)
    }
    form.on('checkbox(roleCode)', function (data) {
        if (data.elem.checked) {
            roleCode.push(data.value);
        } else {
            var index = roleCode.indexOf(data.value);
            roleCode.splice(index, 1)
        }
    });

    var status = resultData.data.status;
    form.on('switch(status)', function (data) {
        if (data.elem.checked) {
            status = 1;
        } else {
            status = 2;
        }
    });

    //获取所有角色码  并渲染
    var ajax = new $ax(system.contextPath + "/api/role.listall");
    ajax.setType("get");
    var result = ajax.start();
    if (result.status) {
        var dataArr = result.data;
        var data = resultData.data.roleList;
        for (var i = 0; i < dataArr.length; i++) {
            var p = dataArr[i].roleCode;
            for (var j = 0; j < data.length; j++) {
                var c = data[j].roleCode;
                if (c === p) {
                    dataArr[i].checked = true;
                    break;
                }
            }
        }
        var data0 = {
            list: dataArr,
        };
        var part0Temp = document.getElementById('part0-template');
        laytpl(part0Temp.innerHTML).render(data0, function (html) {
            document.getElementById("part0").innerHTML = html;
            form.render();
        });
    }

    //回显状态开关按钮
    var data1 = {
        list: [{status: status}],
    };
    var part1Temp = document.getElementById('part1-template');
    laytpl(part1Temp.innerHTML).render(data1, function (html) {
        document.getElementById("part1").innerHTML = html;
        form.render();
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(system.contextPath + "/api/isv.info.update", function (res) {
            if (res.status) {
                system.success("修改成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                system.error("修改失败！" + res.msg)
            }
        }, function (res) {
            system.error("修改失败！" + res.msg)
        });
        data.field.roleCode = roleCode.join(",");
        data.field.status = status;
        data.field.id = id;
        ajax.set(data.field);
        ajax.start();

        return false;
    });
});